#include "AccusationController.h"

#include <boost/bind.hpp>
#include "../views/DependencyContainer.h"

using namespace Controllers;

AccusationController::AccusationController(Players::Player* current_player, Cards::CardsCollection cards,
	Cards::CardSet searched_cards):
current_player(current_player), cards(cards), searched_cards(searched_cards)
{
	view = Views::DependencyContainer::getInstance()->getAccusationViewInstance();
	connections.push_back(view->playerDecisionSetEvent.connect(boost::bind(&AccusationController::onPlayerDecisionSet, this, _1)));
	connections.push_back(view->cardsInAccusationSetEvent.connect(boost::bind(&AccusationController::onCardsInAccusationSet, this, _1)));
}

void AccusationController::perform()
{
	view->askIfPlayerWantsToPerformAccusation(current_player);
}

void AccusationController::onPlayerDecisionSet(bool decision)
{
	if (decision)
	{
		view->askForCardsInAccusation(cards);
	}
	else
	{
		accusationNotPerformedEvent();
	}
}

void AccusationController::onCardsInAccusationSet(Cards::CardSet picked_cards)
{
	if (checkCards(picked_cards))
		accusationCorrectEvent();
	else
		accusationIncorrectEvent();
}


bool AccusationController::checkCards(Cards::CardSet& picked_cards)
{
	if (isAccusationCorrect(picked_cards))
	{
		return true;
	}
	else
	{
		inactivatePlayer();
		return false;
	}
}

bool AccusationController::isAccusationCorrect(Cards::CardSet& picked_cards)
{
	return picked_cards == searched_cards;
}

void AccusationController::inactivatePlayer()
{
	current_player->setInactive();
}

